๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •์‹œ content ๋‚ด์šฉ์ด ๋„˜์–ด๊ฐ€์ง€ ์•Š์Œ.

8/6/2025

๋ฌธ์ œ ์ƒํ™ฉ

์ฝ”๋“œ

"use client";
import Quill from "quill";
import { useEffect, useRef } from "react";
import Editor from "@/app/more/board/write/editor";

export default function EditorWrapper({
  initialValue,
}: {
  initialValue: string;
}) {
  const quillRef = useRef<Quill | null>(null);

  useEffect(() => {
    if (!quillRef.current) return;
    
    try {
      const delta = JSON.parse(initialValue);
      quillRef.current.setContents(delta);
    } catch (error) {
      console.error("Error parsing initialValue", error);
    }
  }, [quillRef.current, initialValue]);

  useEffect(() => {
    const form = document.querySelector("form");
    form?.addEventListener("submit", () => {
      if (quillRef.current) {
        const delta = quillRef.current.getContents();
        const input = document.createElement("input");
        input.type = "hidden";
        input.name = "content";
        input.value = JSON.stringify(delta);
        form.appendChild(input);
      }
    });
  });
  return <Editor ref={quillRef} />;
}

์›์ธ

์™œ content๊ฐ€ ์ถ”๊ฐ€๊ฐ€ ์•ˆ๋˜๋А๋ƒ ํ•˜๋ฉด ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์ œ์ถœ ์ดํ›„์— ๋“ฑ๋ก๋˜์„œ ๊ทธ๋Ÿฐ๋‹ค๊ณ ํ•œ๋‹ค. ๋ฌธ์ œ์ 

  1. ์—ฌ๊ธฐ์— ์˜์กด์„ฑ ๋ฐฐ์—ด์ด ์—†๊ณ 
  2. ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ํ•ด์ œํ•˜์ง€ ์•Š๊ณ  ์ถ”๊ฐ€ํ•˜๊ณ 
  3. 3)์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๋„ˆ๋ฌด ๋Šฆ๊ฒŒ ์‹คํ–‰ํ•œ๋‹ค.

ํ•ด๊ฒฐ

ํ•ด๊ฒฐ์ฑ… : content๋ฅผ ๋‹ด์„ input hidden์„ ๋„ฃ๊ณ , useState๋กœ ๊ฐ’์„ ์ž…๋ ฅํ•ด์ค€๋‹ค.